package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISalaryBillFieldsDAO;
import org.jsola.hr.dto.SalaryBillFieldsAddDTO;
import org.jsola.hr.dto.SalaryBillFieldsUpdateDTO;
import org.jsola.hr.entity.SalaryBillFieldsDO;
import org.jsola.hr.query.SalaryBillFieldsQuery;
import org.jsola.hr.service.ISalaryBillFieldsService;
import org.jsola.hr.vo.SalaryBillFieldsListVO;
import org.jsola.hr.vo.SalaryBillFieldsVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工资条自定义字段
 *
 * @author zhr
 */
@Service("hrSalaryBillFieldsServiceImpl")
public class SalaryBillFieldsServiceImpl implements ISalaryBillFieldsService {

    @Autowired
    private ISalaryBillFieldsDAO salaryBillFieldsDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SalaryBillFieldsVO save(SalaryBillFieldsAddDTO salaryBillFieldsAddDTO, TokenUser tokenUser) {
        // 转DO
        SalaryBillFieldsDO salaryBillFieldsDO = salaryBillFieldsAddDTO.to(SalaryBillFieldsDO.class);
        // 保存
        salaryBillFieldsDO = save(salaryBillFieldsDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return salaryBillFieldsDO.to(SalaryBillFieldsVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SalaryBillFieldsUpdateDTO salaryBillFieldsUpdateDTO, TokenUser tokenUser) {
        //转DO
        SalaryBillFieldsDO salaryBillFieldsDO = salaryBillFieldsUpdateDTO.to(SalaryBillFieldsDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(salaryBillFieldsDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... salaryBillFieldsIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) salaryBillFieldsIds);
    }


    @Override
    public SalaryBillFieldsVO selectById(String salaryBillFieldsId, String siteId) {
        SalaryBillFieldsDO salaryBillFieldsDO = selectDOById(salaryBillFieldsId, siteId);
        if (salaryBillFieldsDO == null) {
            return null;
        }
        return salaryBillFieldsDO.to(SalaryBillFieldsVO.class);
    }

    @Override
    public List<SalaryBillFieldsListVO> select(SalaryBillFieldsQuery salaryBillFieldsQuery, String siteId) {
        List<SalaryBillFieldsDO> salaryBillFieldsDOList = selectDO(salaryBillFieldsQuery, siteId);
        if(CollectionUtils.isEmpty(salaryBillFieldsDOList)) {
            return salaryBillFieldsDOList == null ? null : new ArrayList<>();
        }
        return salaryBillFieldsDOList.stream()
                .map(salaryBillFieldsDO -> salaryBillFieldsDO.to(SalaryBillFieldsListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SalaryBillFieldsQuery salaryBillFieldsQuery, String siteId) {
        Example example = buildExample(salaryBillFieldsQuery,siteId);
        return salaryBillFieldsDAO.selectCountByExample(example);
    }

    @Override
    public Page<SalaryBillFieldsListVO> selectPage(SalaryBillFieldsQuery salaryBillFieldsQuery, String siteId) {
        Example example = buildExample(salaryBillFieldsQuery,siteId);
        Page<SalaryBillFieldsDO> page = salaryBillFieldsDAO.selectPageByExample(example,
                salaryBillFieldsQuery.getPageNo(),
                salaryBillFieldsQuery.getPageSize());

        return page.to(SalaryBillFieldsListVO.class);
    }

    @Override
    public SalaryBillFieldsDO selectDOById(String salaryBillFieldsId, String siteId) {
        return listById(salaryBillFieldsId, siteId);
    }

    @Override
    public List<SalaryBillFieldsDO> selectDO(SalaryBillFieldsQuery salaryBillFieldsQuery, String siteId) {
        Example example = buildExample(salaryBillFieldsQuery,siteId);
        return salaryBillFieldsDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param salaryBillFieldsQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SalaryBillFieldsQuery salaryBillFieldsQuery, String siteId) {
        Example example = new Example(SalaryBillFieldsDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        if (salaryBillFieldsQuery.getBillId() != null) {
            example.and().andEqualTo("billId", salaryBillFieldsQuery.getBillId());
        }
        if (salaryBillFieldsQuery.getFieldName() != null) {
            example.and().andEqualTo("fieldName", salaryBillFieldsQuery.getFieldName());
        }
        if (salaryBillFieldsQuery.getFieldValue() != null) {
            example.and().andEqualTo("fieldValue", salaryBillFieldsQuery.getFieldValue());
        }
        if (salaryBillFieldsQuery.getNeedCalculate() != null) {
            example.and().andEqualTo("needCalculate", salaryBillFieldsQuery.getNeedCalculate());
        }
        if (salaryBillFieldsQuery.getSort() != null) {
            example.and().andEqualTo("sort", salaryBillFieldsQuery.getSort());
        }
        // 排序
        example.orderBy("sort");
        return example;
    }
}




